Dim oAnsoftApp
Dim oDesktop
Dim oProject
Dim oDesign
Dim oEditor
Dim oModule

' Make sure we have the correct number of parameters
if NOT (WScript.Arguments.Count = 7) then
    WScript.Echo "Missing parameters"
    WScript.Quit 1
end if

' Read params
traceLoc = WScript.Arguments(0)
width = WScript.Arguments(1)
length = WScript.Arguments(2)
delta_s = WScript.Arguments(3)
material = WScript.Arguments(4)
thickness = WScript.Arguments(5)
dataRoot = WScript.Arguments(6)

' Get a reference to the Electronics Desktop program
Set oAnsoftApp = CreateObject("Ansoft.ElectronicsDesktop")
Set oDesktop = oAnsoftApp.GetAppDesktop()

' Set correct project as active
Set oProject = oDesktop.SetActiveProject("Unit_Cell_Design")
Set oDesign = oProject.SetActiveDesign(traceLoc & "_Trace")

' Change dogbone width (Wtop1) and length (Ltop1) properties
oDesign.ChangeProperty Array("NAME:AllTabs",_
	Array("NAME:LocalVariableTab",_
		Array("NAME:PropServers", "LocalVariables"),_
            Array("NAME:ChangedProps",_
				Array("NAME:Wtop1", "Value:=", width & "mm"),_
				Array("NAME:Ltop1", "Value:=", length & "mm"))))

oDesign.ChangeProperty Array("NAME:AllTabs",_
	Array("NAME:HfssTab",_
		Array("NAME:PropServers", "AnalysisSetup:Setup1"),_
		Array("NAME:ChangedProps",_
			Array("NAME:Delta S", "Value:=", delta_s & ""))))

' Change substrate thickness
oDesign.ChangeProperty Array("NAME:AllTabs",_
	Array("NAME:LocalVariableTab",_
		Array("NAME:PropServers", "LocalVariables"),_
            Array("NAME:ChangedProps",_
				Array("NAME:Substrate_Thickness", "Value:=", thickness & "mm"))))

' Change substrate Material
Set oEditor = oDesign.SetActiveEditor("3D Modeler")
oEditor.AssignMaterial Array("NAME:Selections", _
		"AllowRegionDependentPartSelectionForPMLCreation:=", true, _
		"AllowRegionSelectionForPMLCreation:=", true, _
		"Selections:=", "Substrate_Top,Substrate_Bottom" _
	), Array("NAME:Attributes", _
		"MaterialValue:=", Chr(34) & "Rogers RO3010 (tm)" & Chr(34), _
		"SolveInside:=", true, _
		"IsMaterialEditable:=", true, _
		"UseMaterialAppearance:=", false, _
		"IsLightweight:=", false)

' Analyze
oDesign.AnalyzeAll

Set oModule = oDesign.GetModule("ReportSetup")

' Delete any old reports
On Error Resume Next
oModule.DeleteReports Array("S_Params")

' Generate report of 2x2 S Matrix
' Families Array: Contains sweep definitions for the report
' Report Data Array:
oModule.CreateReport "S_Params", "Modal Solution Data", "Data Table", "Setup1 : LastAdaptive", Array(), _
        Array("Freq:=", Array("All")), _
        Array("X Component:=", "Freq", "Y Component:=", Array( _
            "re(S(FloquetPort1:1,FloquetPort1:1))", _
            "im(S(FloquetPort1:1,FloquetPort1:1))", _
            "re(S(FloquetPort1:1,FloquetPort2:1))", _
            "im(S(FloquetPort1:1,FloquetPort2:1))", _
            "re(S(FloquetPort2:1,FloquetPort1:1))", _
            "im(S(FloquetPort2:1,FloquetPort1:1))", _
            "re(S(FloquetPort2:1,FloquetPort2:1))", _
            "im(S(FloquetPort2:1,FloquetPort2:1))")), Array()

oModule.ExportToFile "S_Params", dataRoot&"\\sim_results.csv"
oDesign.ExportConvergence "Setup1", "", dataRoot&"\\convergence.conv"